﻿using System.Windows.Media;
using System.Windows.Media.Imaging;
using FanaticRock.AudioManage.Helpers;
using FanaticRock.Core.Models;
using FanaticRock.DownloadManage.Helpers;

namespace FanaticRock.Helpers
{
    public static class ResourceSourceHelper
    {
        public static Uri GetUri(ResourceSource resourceSource)
        {
            if (resourceSource is UriResourceSource uriResourceSource)
            {
                return uriResourceSource.Uri;
            }
            return null;
        }

        public static ImageBrush CreateImageBrush(ResourceSource resourceSource)
        {
            var imageSource = CreateImageSource(resourceSource);
            if (imageSource == null) return null;
            var imageBrush = new ImageBrush(imageSource)
            {
                Stretch = Stretch.UniformToFill,
            };
            return imageBrush;
        }

        ///<summary>获得图片。</summary>
        public static BitmapSource CreateImageSource(ResourceSource resourceSource)
        {
            if (resourceSource is UriResourceSource uriResourceSource)
            {
                return CreateImageSourceCore(uriResourceSource);
            }
            else if (resourceSource is BytesResourceSource bytesResourceSource)
            {
                return CreateImageSourceCore(bytesResourceSource);
            }
            else if (resourceSource is InAudioFileResourceSource inAudioFileResourceSource)
            {
                return CreateImageSourceCore(inAudioFileResourceSource);
            }
            throw new NotImplementedException($"ResourceSourceHelper.CreateImageSource");
        }

        ///<summary>获得字节。</summary>
        public static async Task<byte[]> GetBytes(ResourceSource resourceSource)
        {
            if (resourceSource is UriResourceSource uriResourceSource)
            {
                return await GetBytesCore(uriResourceSource);
            }
            else if (resourceSource is BytesResourceSource bytesResourceSource)
            {
                return GetBytesCore(bytesResourceSource);
            }
            else if (resourceSource is InAudioFileResourceSource inAudioFileResourceSource)
            {
                return GetBytesCore(inAudioFileResourceSource);
            }
            throw new NotImplementedException($"ResourceSourceHelper.GetBytes");
        }

        ///<summary>获得字节Core</summary>
        private static async Task<byte[]> GetBytesCore(UriResourceSource resourceSource)
        {
            return await DownloadBytesHelper.GetBytes(resourceSource.Uri);
        }

        ///<summary>获得字节Core</summary>
        private static byte[] GetBytesCore(BytesResourceSource resourceSource)
        {
            return resourceSource.Bytes;
        }

        ///<summary>获得字节Core</summary>
        private static byte[] GetBytesCore(InAudioFileResourceSource resourceSource)
        {
            return AudioFormatHelper.TryGetCover(resourceSource.FileUri);
        }

        private static BitmapSource CreateImageSourceCore(UriResourceSource resourceSource)
        {
            var uri = resourceSource.Uri;
            if (uri == null) return null;
            var imageSource = new BitmapImage();
            //imageSource.DownloadCompleted += ImageSource_DownloadCompleted;
            imageSource.BeginInit();

            imageSource.DecodePixelWidth = 200; // 可选：设置解码后的宽度以减少内存占用
            imageSource.CacheOption = BitmapCacheOption.OnLoad; // 加载完成后释放内存缓存
            imageSource.CreateOptions = BitmapCreateOptions.IgnoreImageCache; // 可选：不使用缓存，强制重新下载
            imageSource.UriSource = uri;

            imageSource.EndInit();

            return imageSource;
        }

        private static void ImageSource_DownloadCompleted(object? sender, EventArgs e)
        {
            if (sender is BitmapImage bitmapImage)
            {
                bitmapImage.DownloadCompleted -= ImageSource_DownloadCompleted;
                if (bitmapImage.CanFreeze)
                    bitmapImage.Freeze();
            }
        }

        private static BitmapSource CreateImageSourceCore(BytesResourceSource resourceSource)
        {
            var bytes = resourceSource.Bytes;
            var imageSource = Theme.Helpers.ImageHelper.CreateImageSource(bytes, Theme.Helpers.ImageQuality.Max);
            return imageSource;
        }

        private static BitmapSource CreateImageSourceCore(InAudioFileResourceSource resourceSource)
        {
            var bytes = AudioFormatHelper.TryGetCover(resourceSource.FileUri);
            var imageSource = Theme.Helpers.ImageHelper.CreateImageSource(bytes, Theme.Helpers.ImageQuality.Max);
            return imageSource;
        }
    }
}
